package org.tgit.gateway.config;

import java.time.Duration;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;



/**
 * 
 * @author fanzaijun
 * 2017年6月26日
 */
@Configuration
@EnableCaching //标注启动缓存
public class CacheConfig {
	
	/*
	 * 据shared与否的设置,
	 * Spring分别通过CacheManager.create()
	 * 或new CacheManager()方式来创建一个ehcache基地.
	 * 也说是说通过这个来设置cache的基地是这里的Spring独用,还是跟别的(如hibernate的Ehcache共享)
	 */
	@Bean
	public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
		EhCacheManagerFactoryBean cacheManagerFactoryBean=new EhCacheManagerFactoryBean();
		cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
		cacheManagerFactoryBean.setShared(true);
		return cacheManagerFactoryBean;
	}
	
	 /**
     * ehcache 主要的管理器
     * @param  bean
     * @return
     */
	@Bean(name="gatewayCacheManager")
	@ConditionalOnProperty(value="spring.cache.type",havingValue="ehcache")
	public CacheManager  ehCacheCacheManager(EhCacheManagerFactoryBean  bean){
		return new EhCacheCacheManager(bean.getObject());
	}
	
	/**
	 * 
	* @Title: cacheManager   
	* @Description: 缓存管理
	* @author fanzaijun  
	* @date 2018年11月12日 下午3:39:10   
	* @param connectionFactory
	* @return  
	* RedisCacheManager  
	* @throws (没有异常抛出)
	 */
	@Bean(name="gatewayCacheManager")
	@ConditionalOnProperty(value="spring.cache.type",havingValue="redis")
    public CacheManager redisCacheManager(RedisConnectionFactory connectionFactory){
		RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时
		// 设置一个初始化的缓存空间set集合
	    Set<String> cacheNames =  new HashSet<>();
	    cacheNames.add("TgitGatewayCache");
        return RedisCacheManager
                .builder(RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory))
                .initialCacheNames(cacheNames) 
                .cacheDefaults(redisCacheConfiguration).build();
    }

	
	@Bean(name="gatewayCache")
	@ConditionalOnProperty(value="spring.cache.type",havingValue="ehcache")
	public Cache initEhcache(CacheManager gatewayCacheManager){
		return gatewayCacheManager.getCache("TgitGatewayCache");
	}
	
	@Bean(name="gatewayCache")
	@ConditionalOnProperty(value="spring.cache.type",havingValue="redis")
	public Cache initRedisCache(CacheManager gatewayCacheManager){
		return gatewayCacheManager.getCache("TgitGatewayCache");
	}
	
	
	
}
